package com.shcz.migu.utils;

import android.annotation.SuppressLint;
import android.os.Build;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * 日志文件类，初始化时上传上次的日志文件到服务器
 * 
 * @author Jone
 *
 */
@SuppressLint("SimpleDateFormat")
public class LogcatStorageHelper {

    private final static int MAX_RETRY_TIMES = 1;
    private final static String tag = "LogcatStorageHelper";
    private static LogcatStorageHelper INSTANCE = null;
    private static String PATH_LOGCAT;

//    private Context context;
    /**
     *  
     * 初始化目录 
     *  
     * */  
    public void init() {
//		this.context = context;
        PATH_LOGCAT = CommonUtils.getAppCacheDirPath() + "/logs/";
        dir = PATH_LOGCAT;
        File file = new File(PATH_LOGCAT);
        if (!file.exists()) {  
            file.mkdirs();  
        }  
    }  
  
    public static LogcatStorageHelper getInstance() {
        if (INSTANCE == null) {
            synchronized (LogcatStorageHelper.class) {
                if (INSTANCE == null) {
                    INSTANCE = new LogcatStorageHelper();
                }
            }
        }
        return INSTANCE;  
    }

    public void addLog(String logData){
        if (out == null) {
            if (!initFile()) {
                return;
            }
        }

        try {
            String data = logData;

            if (data != null && data.length() > 0 && out != null) {
                out.write((getDateEN() + "  " + data + "\n")
                        .getBytes());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
  
    private LogcatStorageHelper() {
        init();
    }
  

    private void checkUploadDebugFile(){
    	File dir = new File(PATH_LOGCAT);
    	String[] files = dir.list();
    	if(files != null && files.length > 0){
//    		uploadDebugFile(files);
    	}
    }

    private String dir;
    private FileOutputStream out = null;
    private String curFileName;

    private boolean initFile(){
        try {
            File outFile = new File(dir, getFileName() + ".log");
            int i = 1;
            while(outFile.exists()){
                outFile = new File(dir, getFileName() + "-" + (i++) + ".log");
            }
            curFileName = outFile.getName();
            out = new FileOutputStream(outFile);

            if(out != null){
                try {
                    out.write("****************************************************************************\n".getBytes());
                    out.write((getDateEN() + "  " + Build.MODEL  + "  SDK VERSION: " + Build.VERSION.RELEASE +
                            "  APP VERSION: " + CommonUtils.getAppVersionName(CommonUtils.getAppContext()) + "\n").getBytes());
                    out.write("****************************************************************************\n".getBytes());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return false;
        }

        return true;
    }


    @SuppressLint("SimpleDateFormat")
    public static String getFileName() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String date = format.format(new Date(System.currentTimeMillis()));
        return date;// 2012年10月03日 23:41:31  
    }  
  
    public static String getDateEN() {
        SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        String date1 = format1.format(new Date(System.currentTimeMillis()));
        return date1;// 2012-10-03 23:41:31  
    }

}